<template>
    <div class="mod-config">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-select
            clearable
            v-model="dataForm.projectId"
            placeholder="所属项目"
          >
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            align="right"
            @change="timeChange"
            clearable
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <!-- <el-button
            type="danger"
            v-if="isAuth('generator:bcarledger:delete')"
            @click="deleteHandle()"
            :disabled="dataListSelections.length <= 0"
            >批量删除</el-button
          > -->
          <el-button
            v-if="isAuth('generator:bcarledger:handleExport')"
            @click="handleExport2()"
            >导出</el-button
          >
          <el-button
          @click="printBtn()"
            >打印</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle2"
        style="width: 100%;"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        >
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          header-align="center"
          align="center"
          width="50"
        >
        </el-table-column>
        <el-table-column
          prop="carCode"
          header-align="center"
          align="center"
          label="车牌号"
        >
        </el-table-column>
        <el-table-column
          prop="sum"
          header-align="center"
          align="center"
          label="总车次"
        >
          <template slot-scope="scope">
            <span
              >{{ scope.row.sum }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="normal"
          header-align="center"
          align="center"
          label="正常车次"
        >
        </el-table-column>
        <el-table-column
          prop="abnormal"
          header-align="center"
          align="center"
          label="异常车次"
        >
        </el-table-column>
        <!-- <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="checkDetailed(scope.row.id)"
              >查看明细</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageParams.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageParams.limit"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
      <!-- <el-dialog title="总车次" :visible.sync="dialogTableVisible">
        <el-date-picker
          v-model="dateRangeAll"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          @change="timeChangeitem"
          clearable
        >
        </el-date-picker>
        <el-button
            @click="handleExport2()"
            >导出</el-button
          >
        <el-table
          style="margin-top: 20px;"
          :data="gridData"
          @selection-change="selectionChangeHandle2"
        >
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"
          ></el-table-column>
          <el-table-column property="carCode" label="车牌"></el-table-column>
          <el-table-column property="sum" label="总车次"></el-table-column>
          <el-table-column property="normal" label="正常"></el-table-column>
          <el-table-column property="abnormal" label="异常"></el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle2"
          @current-change="currentChangeHandle2"
          :current-page="pageParams2.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageParams2.limit"
          :total="totalPage2"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </el-dialog> -->
  
      <!-- 弹窗, 新增 / 修改 -->
      <leader-track v-if="trackVisible" ref="leaderTrack"></leader-track>
      <leader-info v-if="infoVisible" ref="leaderInfo"></leader-info>
    </div>
  </template>
  
  <script>
  import leaderTrack from "./leaderTrack.vue";
  import leaderInfo from "./leaderInfo.vue";
  export default {
    data() {
      return {
        dialogTableVisible: false,
        gridData: [],
        projectList: [],
        dataForm: {
          projectId: "",
          endDate: new Date(parseInt(+new Date()))
            .toLocaleString()
            .replace(/[/]+/g, "-"),
          startDate: new Date(parseInt(+new Date() - 3600 * 1000 * 24))
            .toLocaleString()
            .replace(/[/]+/g, "-")
        },
        dataList: [],
        pageParams: {
          page: 1,
          limit: 10
        },
        pageParams2: {
          page: 1,
          limit: 10,
          projectId: "",
          endDate: new Date(parseInt(+new Date()))
            .toLocaleString()
            .replace(/[/]+/g, "-"),
          startDate: new Date(parseInt(+new Date() - 3600 * 1000 * 24))
            .toLocaleString()
            .replace(/[/]+/g, "-")
        },
        totalPage: 0,
        totalPage2: 0,
        dataListLoading: false,
        dataListSelections: [],
        dataListSelections2: [],
        addOrUpdateVisible: false,
        trackVisible: false,
        infoVisible: false,
        statusList: [
          {
            label: "异常",
            value: 0
          },
          {
            label: "合格",
            value: 1
          }
        ],
  
        dateRange: [
          new Date(parseInt(+new Date() - 3600 * 1000 * 24))
            .toLocaleString()
            .replace(/[/]+/g, "-"),
          new Date(parseInt(+new Date())).toLocaleString().replace(/[/]+/g, "-")
        ],
        dateRangeAll: [
          new Date(parseInt(+new Date() - 3600 * 1000 * 24))
            .toLocaleString()
            .replace(/[/]+/g, "-"),
          new Date(parseInt(+new Date())).toLocaleString().replace(/[/]+/g, "-")
        ]
      };
    },
    components: { leaderTrack, leaderInfo },
    activated() {
      this.getDataList();
    },
    mounted() {
      this.getProject();
    },
    methods: {
      // 打印
      printBtn(){
        window.print()
      },
      timeChange(e) {
        if (e) {
          this.dataForm.startDate = e[0];
          this.dataForm.endDate = e[1];
        } else {
          this.dataForm.startDate = null;
          this.dataForm.endDate = null;
        }
      },
  
      // 查看异常
      infoHandle(id) {
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.leaderInfo.init(id);
        });
      },

      // 导出弹框内容
      handleExport2(){
        let ids;
        if (this.dataListSelections2.length === 0) {
          ids = this.dataList.map(item => {
            return item.id;
          });
        } else {
          ids = this.dataListSelections2.map(item => {
            return item.id;
          });
        }
        if (ids.length > 0) {
          this.$http({
            url: this.$http.adornUrl(`/sys/car/report/car/handleExport?ids=${ids}`),
            method: "get",
            responseType: "blob"
          }).then(({ data }) => {
            console.log(data, "data");
            const blob = new Blob([data], { type: "application/vnd.ms-excel" });
            let a = document.createElement("a");
            a.download = "车次报表.xlsx";
            a.href = URL.createObjectURL(blob);
            document.getElementsByTagName("body")[0].appendChild(a);
            a.click();
            a.parentNode.removeChild(a);
          });
        }
      },
      // 获取所属项目
      getProject() {
        this.$http({
          url: this.$http.adornUrl(`/sys/project/management/list`),
          method: "get"
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.projectList = data.list;
          }
        });
      },
      selectionChangeHandle2(val) {
      this.dataListSelections2 = val;
    },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        let params = { ...this.pageParams, ...this.dataForm };
        this.$http({
          url: this.$http.adornUrl("/sys/car/report/car/page"),
          method: "get",
          params: this.$http.adornParams(params)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageParams.limit = val;
        this.pageParams.page = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageParams.page = val;
        this.getDataList();
      },
      // 删除
      deleteHandle(id) {
        var ids = id
          ? [id]
          : this.dataListSelections.map(item => {
              return item.id;
            });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/car/report/delete"),
            method: "delete",
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },
  
      //查看明细
      checkDetailed(id) {
        this.$router.push({
          path: "management-ledgerManagement",
          query: {
            reportId: id
          }
        });
      }
    }
  };
  </script>
  